// 导入数据库模块
const db = require('../DB/index');

// 查询所有审批组数据
exports.getApprover=(req,res)=>{
    const sql = `SELECT approver.id,approver.name,approver.town_id,town.town_name FROM approver 
    ,town WHERE approver.town_id = town.id`
    db.query(sql,(err,result)=>{
        if(err) return res.cc(err)
        res.send({
            status:0,
            message:'获取信息成功',
            data:result
        })
    })
}

// 根据区域id获取审批组信息
exports.idToApprover = (req,res)=>{
    const obj = req.body
    const sql = `SELECT * FROM approver WHERE town_id = ${obj.id}`
    db.query(sql,(err,result)=>{
        if(err) return res.cc(err)
        res.send({
            status:0,
            message:'获取用户成功',
            data:result
        })
    })
}

// 根据id获取审批组信息
exports.idApprover = (req,res)=>{
    const obj = req.body
    const sql = `SELECT * FROM approver WHERE id = ${obj.approver_id}`
    db.query(sql,(err,result)=>{
        if(err) return res.cc(err)
        res.send({
            status:0,
            message:'获取用户成功',
            data:result[0]
        })
    })
}
// 新增审批组
exports.addApprover = (req,res)=>{
    const obj = req.body
    const sql = `INSERT INTO approver SET ?`
    db.query(sql,{name:obj.name,town_id:obj.town_id},(err,result)=>{
          // 判断sql语句是否执行成功
          if(err) return res.cc(err)
          // 判断影响函数是否为1
          if(result.affectedRows !== 1) return res.cc('新增失败！')
          res.cc('新增成功' ,0)
    })
}

// 修改审批组
exports.updateApprover = (req,res) =>{
    const obj = req.body
    const sql = `UPDATE approver SET name = '${obj.name}',town_id = ${obj.town_id} WHERE id = ${obj.id}`

    db.query(sql,(err,result)=>{
        if(err) return res.cc(err)
        if(result.affectedRows !== 1) return res.cc('修改失败')
        res.cc('修改成功',0)
    })
}

// 删除审批组
exports.deleteApprover = (req,res)=>{
    const obj = req.body
    const sql = `DELETE FROM approver WHERE id = ${obj.id}`
    db.query(sql,(err,result)=>{
        if(err) return res.cc(err)
        if(result.affectedRows !== 1) return res.cc('删除失败')
        res.cc('删除成功',0)
    })
}

// 根据用户id获取获取其请假记录
exports.getLeave = (req,res)=>{
    const obj = req.body
    const sql = `SELECT * FROM leaveInfo WHERE user_id = ${obj.id}`
    db.query(sql,(err,result)=>{
        if(err) return res.cc(err)
        res.send({
            status:0,
            data:result
        })
    })
}

// 新增请假信息
exports.addLeave = (req,res)=>{
    const obj = req.body
    const sql = `INSERT INTO leaveInfo SET ?`
    db.query(sql,{
        user_id:obj.user_id,
        approver_id:obj.approver_id,
        startTime:obj.startTime,
        endTime:obj.endTime,
        approver_user_id:obj.approver_user_id,
        leaveType:obj.type,
        type: 0 // 默认审批中
    },(err,result)=>{
        if(err) return res.cc(err)
        if(result.affectedRows !== 1) return res.cc('添加失败')
        res.cc('添加成功',0)
    })
}

// 根据审批员id获取其审批组下的请假记录
exports.getApprovers = (req,res)=>{
    const obj = req.body
    const sql = `SELECT leaveinfo.*,user.name FROM leaveinfo,user WHERE approver_user_id = ${obj.approver_user_id} AND user.id = leaveinfo.user_id`
    db.query(sql,(err,result)=>{
        if(err) return res.cc(err)
        res.send({
            status:0,
            data:result
        })
    })
}

// 审批请假记录
exports.approver = (req,res) => {
    const obj  = req.body
    const sql = `UPDATE leaveinfo SET type = ${obj.type} WHERE id = ${obj.id}`
    db.query(sql,(err,result)=>{
        if(err) return res.cc(err)
        if(result.affectedRows !== 1) return res.cc('审核失败')
        res.cc('审批成功',0)
    })
}